package com.yn.club.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.mapstruct.Named;
import org.springframework.util.StringUtils;

/**
 * @author Bing
 */
public class MapStructUtil {

  @Named("listToStr")
  public static <T> String listToStr(List<T> list) {
    if (list == null) {
      return null;
    } else {
      return list.stream().filter(Objects::nonNull)
          .map(Object::toString)
          .collect(Collectors.joining(","));
    }
  }

  @Named("strToIntList")
  public static List<Integer> strToIntList(String list) {
    if (StringUtils.hasText(list)) {
      return Arrays.stream(list.split(","))
          .map(i -> Integer.parseInt(i.trim()))
          .collect(Collectors.toList());
    } else {
      return new ArrayList<>();
    }
  }

  @Named("strToLongList")
  public static List<Long> strToLongList(String list) {
    if (StringUtils.hasText(list)) {
      return Arrays.stream(list.split(","))
          .map(i -> Long.parseLong(i.trim()))
          .collect(Collectors.toList());
    } else {
      return new ArrayList<>();
    }
  }

  @Named("strToFloatList")
  public static List<Float> strToFloatList(String list) {
    if (StringUtils.hasText(list)) {
      return Arrays.stream(list.split(","))
          .map(i -> Float.parseFloat(i.trim()))
          .collect(Collectors.toList());
    } else {
      return new ArrayList<>();
    }
  }

  @Named("strToDoubleList")
  public static List<Number> strToDoubleList(String list) {
    if (StringUtils.hasText(list)) {
      return Arrays.stream(list.split(","))
          .map(i -> Double.parseDouble(i.trim()))
          .collect(Collectors.toList());
    } else {
      return new ArrayList<>();
    }
  }

  @Named("strToList")
  public static List<String> strToList(String list) {
    if (StringUtils.hasText(list)) {
      return Stream.of(list.split(",")).collect(Collectors.toList());
    } else {
      return new ArrayList<>();
    }
  }
}
